<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/**
* Name:  Blog Model
*
* Author:  William Bermudo
*
*/

class Blog_categories_m extends DataMapperExt
{
	public $table = 'posts_categories';
	
	public $has_many = array(
						'blogs'	=> array(
							'class'		   => 'blog_m',
							'other_field'  => 'category',
							'join_self_as' => 'category',
							'join_table'   => 'posts'
						),
						'posts'	=> array(
							'class'		   => 'post_m',
							'other_field'  => 'category',
							'join_self_as' => 'category',
							'join_table'   => 'posts'
						)
					);
	
	public function __construct($id = NULL)
	{
		parent::__construct($id);
	}
	
	public function get_categories()
	{
		$oCategory = new Blog_categories_m();
		$oCategory->where('is_active', '1');
		$oCategory->get();
		
		if ($oCategory->result_count() > 0)
		{
			$categories = array('0' => '-- None --');
			foreach($oCategory AS $category)
			{
				$categories[$category->id] = $category->title;
			}
			
			return $categories;
		}
	}
	
	public function create_category($title)
	{
		$oCategory = new Blog_categories_m();
		$oCategory->title 	= $title;
		$oCategory->url		= strtolower(str_replace(' ', '-', $title));
		$oCategory->save();
		
		return TRUE;
	}
}